/**  
 * Copyright © 2017武汉思恒达科技有限公司. All rights reserved.
 * @Title: POIFileUtil.java
 * @Prject: ads
 * @Package: com.loongtek.onyx.corgi.web.ads.utils
 * @author: int_java_se@163.com
 * @date: 2017年3月7日 下午2:33:07
 */
package com.thinkingstar.iads.common.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;

/**
 * @ClassName: POIFileUtil <br/> 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @date: 2017年3月7日 下午2:33:07 <br/> 
 * @author int_java_se@163.com
 * @version  
 * @since JDK 1.7
 */
public class POIFileUtil {
	
	//文件盘符
	private static final String system_path = InitSystemProperties.getSystemPath();
	
	//项目根目录
	private static final String system_file_path = InitSystemProperties.getSystemFilePath();
	
	//jar相对路径
	private static final String jar_file_path = InitSystemProperties.getJarFilePath(); 
	
	//业务目录
	private static final String business_path= InitSystemProperties.getBusinessPath();
	

	/**
	 * 创建临时文件
	 * @param wb
	 * @param fileName
	 * @return
	 */
	public static File createTempFile(Workbook wb,String fileName){
		String filePath = system_path+File.separator+system_file_path+File.separator+"temp"+File.separator;
		filePath += fileName;
		File file = createFile(wb,filePath);
		return file;
	}
	
	//创建压缩文件
    public static Boolean writeZip(List<File> file_list,String zipname) {
        ZipOutputStream zos = null;
    	try {
    		OutputStream os = new BufferedOutputStream( new FileOutputStream( zipname ) );
    		zos = new ZipOutputStream(os);
            byte[] buf = new byte[8192];
            int len;
            for (int i=0;i<file_list.size();i++) {  
                File file = file_list.get(i);
                ZipEntry ze = new ZipEntry( file.getName() );
                zos.putNextEntry( ze );
                BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );
                while ( ( len = bis.read( buf ) ) > 0 ) {
                    zos.write( buf, 0, len );
                }
            }
            return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			if(zos != null){
	            try {
					zos.closeEntry();
					zos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

    }
	
	/**
	 * 创建文件
	 * @param fileName
	 * @param fileName
	 * @return
	 */
	public static String getFileName(String fileName , String suffix_name , String file_type){
		String filePath = file_type+File.separator+suffix_name+File.separator+fileName;
		return filePath;
	}
	
	
	
	/**
	 * 创建提入资料非结构化数据存储路径
	 * @param fileName
	 * @return
	 */
	public static String getMentionFileName(String fileName , String suffix_name , String project_id){
		String filePath = "mention"+File.separator+"notStructur"+File.separator+suffix_name+File.separator+project_id+File.separator+fileName;
		return filePath;
	}
	
	
	
	/**
	 * 创建结构化提入资料导出的excel临时文件地址
	 * @param fileName
	 * @return
	 */
	public static String getExportExcelFileName(String fileName){
		String filePath = "mention"+File.separator+"structur"+File.separator+"exportExcel"+File.separator+fileName;
		return filePath;
	}

	/**
	 * 创建设计单元导出的excel临时文件地址
	 * @param fileName
	 * @return
	 */
	public static String getDesignCellExportExcelFileName(String fileName){
		String filePath = "designcell"+File.separator+"exportExcel"+File.separator+fileName;
		return filePath;
	}

	public static String getFilePathByType(String type,String project_id) {
		String path = InitSystemProperties.getSystemPath()+File.separator+"iads"+File.separator+project_id+File.separator;
		String path2 = InitSystemProperties.getSystemPath()+File.separator+InitSystemProperties.getSystemFilePath()+File.separator+InitSystemProperties.getBusinessPath()+File.separator;
		if (type.equals("提入资料_jgh")) {
			return path2+"mention"+File.separator+"export"+File.separator;
		} else if (type.equals("提入资料_fjgh")) {
			return path2;
		} else if (type.equals("设计单元")) {
			return path+"designcell"+File.separator+"export"+File.separator;
		} else if(type.equals("设计评审")){
			return path2+"sjps"+File.separator+"export"+File.separator+project_id+File.separator;
		}  else if(type.equals("提出资料")){
			return path2+"tczl"+File.separator+"export"+File.separator+project_id+File.separator;
		} else if (type.equals("设备配备")){
			return path+"sbpb"+File.separator+"export"+File.separator;
		} else if (type.equals("设备概算")){
			path = InitSystemProperties.getSystemPath()+File.separator+InitSystemProperties.getSystemFilePath()+File.separator+InitSystemProperties.getBusinessPath()+File.separator;
			return path+"equipmentBudget"+File.separator;
		} else{
			return "";
		}
	}


	/**
	 * 创建提入资料上传的excel临时文件地址
	 * @param fileName
	 * @return
	 */
	public static String getUploadExcelFileName(String fileName){
		String filePath = "mention"+File.separator+"structur"+File.separator+"uploadExcel"+File.separator+fileName;
		return filePath;
	}

	/**
	 * 获取项目绝对路径
	 * @param project_id
	 * @return
	 */
	public static String getRepath(String project_id){
		String filePath = "D:"+File.separator+"iads"+File.separator+project_id+File.separator+"temp"+File.separator;
		return filePath;
	}

	/**
	 * 根据相对路径获取绝对路径
	 * @param repath
	 * @return
	 */
	public static String getAbFileNameByRepath(String repath){
		String filePath = system_path+File.separator+system_file_path+File.separator+business_path+File.separator+repath;
		return filePath;
	}

	/**
	 * 根据相对路径获取绝对路径不需要文件的根 比如/opt   或者 d:
	 * 在pdf等文件预览过程中，预览服务的配置，需要文件的根，这里去除掉
	 * 一般文件的根为  /opt   windows上为  d:
	 * @param repath
	 * @return
	 */
	public static String getFileURIPathByRepath(String repath){
		String filePath = File.separator+jar_file_path+File.separator+system_path+File.separator+system_file_path+File.separator+business_path+File.separator+repath;
		return FileCommonUtil.pathToURI(filePath);
	}
	
	/**
	 * @Title: createFile 
	 * @Description: 创建文件通用方法
	 * @param @param wb
	 * @param @param filePath 
	 * @param @param fileName
	 * @param @return
	 */
	public static File createFile(Workbook wb,String filePath){
			FileOutputStream fos = null;
			File inputFile = null;
		try {
			System.out.println("create file 1 --------------------------------"+filePath+"------------");
			inputFile = new File(filePath).getParentFile();
	        if (!inputFile.exists()) {
	        	inputFile.mkdirs();
	        }
	        fos = new FileOutputStream(filePath);
	        wb.write(fos);
	        fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new File(filePath);
	}
	
	
	//将json格式数据写入文件
	public static File createJsonFile(String json_data,String filePath){
		BufferedWriter writer = null;
		File inputFile = null;
	try {
		inputFile = new File(filePath).getParentFile();
        if (!inputFile.exists()) {
        	inputFile.mkdirs();
        }
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath),false), "UTF-8"));
        writer.write(json_data);
	} catch (IOException e) {
		e.printStackTrace();
	}finally {
		if(writer != null){
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	return new File(filePath);
}
	
	
	/**
	 * @Title: createFile 
	 * @Description: 创建文件通用方法
	 * @param @param wb
	 * @param @param filePath 
	 * @param @param fileName
	 * @param @return
	 */
	public static File createFile(XWPFDocument wb,String filePath,String fileName){
			FileOutputStream fos = null;
			File file = null;
		try {
			System.out.println("create file --------------------------------"+filePath+fileName+"------------");
			File inputFile = new File(filePath);
	        if (!inputFile.exists()) {
	        	inputFile.mkdirs();
	        }
	        fos = new FileOutputStream(filePath+fileName);
	        wb.write(fos);
	        fos.close();
	        file = new File(filePath+fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}
	

}
